package com.person.jc.service.template;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.service.Service;
import com.person.jc.service.car.BdCarLogService;
import com.person.jc.utils.Identities;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ailk on 2017/7/15.
 */
public class TemplateService extends Service {

    public static final TemplateService me = new TemplateService();

    public Record findTempInfoByEquId(String equId){
        String sql="SELECT bc.cust_name, boe.device_name, boe.device_manufactor, boe.device_model, boe.device_no, boe.look_status, boe.device_type FROM bs_order_equipment boe INNER JOIN bs_order bo ON boe.order_id = bo.order_id INNER JOIN bd_customer bc ON bo.cust_id = bc.cust_id WHERE boe.equ_id = ?";

       return Db.findFirst(sql,equId);
    }

//    public List<Record> findBzwzByScopeId(String scopeId){
//        String sql = "select sd.* from bd_device_relation dr inner join bd_standard_device sd on dr.device_id_=sd.device_id where dr.device_type=0 and dr.scope_id=? order by dr.sort";
//        return Db.find(sql,scopeId);
//    }
        public List<Record> findBzwzByScopeId(String scopeId){
            String sql = "select sm.* from bd_device_relation dr inner join bd_standard_matter sm on dr.device_id_=sm.matter_id where dr.device_type=0 and dr.scope_id=? order by dr.sort";
            return Db.find(sql,scopeId);
        }

        public Record findBSReconditionLog(String orderId,String equId){
            String sql="select * from bs_recondition_log where order_id=? and equ_id=?";
            return Db.findFirst(sql,orderId,equId);
        }

        public void updateBSReconditionLog(String orderId,String equId ,String fileName,String isOk,String scopes){
            String sql="update bs_recondition_log set original_record = ? ,original_record_ok=?,scopes=?  where order_id=? and equ_id=?";
            Object[] obj = new Object[5];
            obj[0] = fileName;
            obj[1] = isOk;
            obj[2] = scopes;
            obj[3] = orderId;
            obj[4] = equId;
            Db.update(sql,obj);

        }

        public void saveBsCert(Map<String,Object> map){
            //先判断证书是否有保存
            Record rec=Db.findFirst("select id from bs_certificate where order_id=? and equ_id=?",map.get("orderId"),map.get("equId"));
            if(rec!=null && rec.getInt("id")!=null){
                Db.update("update bs_certificate set is_passed=? , save_path=?, opt_user=?, opt_date=now() where order_id=? and equ_id=?",map.get("isPassed"),map.get("savePath"),map.get("optUser"),map.get("orderId"),map.get("equId"));
            }else{

                Record record = new Record();
                record.set("order_id", map.get("orderId"));
                record.set("equ_id", map.get("equId"));
                record.set("is_passed", map.get("isPassed"));
                record.set("save_path", map.get("savePath"));
                record.set("opt_user", map.get("optUser"));
                record.set("opt_date", new Date());
                Db.save("bs_certificate", record);
            }

        }


        public Record findCertFrontInfoByOrderId(String orderId,int equId ){
           String sql="SELECT\n" +
                   "c.cust_name,\n" +
                   "oe.device_name,\n" +
                   "oe.device_model,\n" +
                   "oe.device_no,\n" +
                   "oe.device_manufactor\n" +
                   "FROM\n" +
                   "bs_order o\n" +
                   "INNER JOIN bs_order_equipment oe ON oe.order_id = o.order_id\n" +
                   "INNER JOIN bd_customer c ON c.cust_id = o.cust_id\n" +
                   "WHERE\n" +
                   "o.order_id = ?\n" +
                   "AND oe.equ_id = ?";

          return  Db.findFirst(sql,orderId,equId);
        }


        public void saveOriginalRecordKeyValue(String orderId,String equId,String type,String isPass, String keyValue,String userId,String checkUser,String verifiedUser,String verify){
            Record record=Db.findFirst("select id from bs_certificate_key_value where order_id=? and equ_id=? and type=?",orderId,equId,type);
            if(record!=null && record.getInt("id")!=null){
                Db.update("update bs_certificate_key_value set key_value=?, is_pass=?, opt_date=now(), opt_user=?, check_user=?,verified_user=?, verify=? where order_id=? and equ_id=? and type=?",keyValue,isPass,userId,checkUser,verifiedUser,verify,orderId,equId,type);
            }else{
                Record rec = new Record();
                rec.set("order_id", orderId);
                rec.set("equ_id", equId);
                rec.set("type", type);
                rec.set("opt_date", new Date());
                rec.set("opt_user", userId);
                rec.set("key_value", keyValue);
                rec.set("is_pass", isPass);
                rec.set("check_user", checkUser);
                rec.set("verified_user", verifiedUser);
                rec.set("verify", verify);

                Db.save("bs_certificate_key_value", rec);
            }
        }

}

